package cn.zc.kafkademo.consumer;

import cn.zc.kafkademo.common.KafkaTopicConstants;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

//消费者
@Component
public class KafkaConsumer {
    //@KafkaListener(topics = {"#{'${kafka.topic1}'.split(',')}"})  // 多个topic，用逗号隔开
    @KafkaListener(topics = {"${kafka.topic1}"},groupId = "${kafka.consumer.group.id}",containerFactory = "kafkaListenerContainerFactory")  // 单个topic  ,groupId = "${kafka.consumer.group.id}",containerFactory = "kafkaListenerContainerFactory"
    //@KafkaListener(topics = {KafkaTopicConstants.MY_TOPIC})
    public void receiveMessage(ConsumerRecord<?,?> consumerRecord){
        System.out.println(consumerRecord.key() + ": message1" + consumerRecord.value());
    }

    @KafkaListener(topics = {"${kafka.topic1}"},groupId = "${kafka.consumer2.group.id}",containerFactory = "kafkaListenerContainerFactory2")
    public void receiveMessage2(ConsumerRecord<?,?> consumerRecord){
        System.out.println(consumerRecord.key() + ": message2" + consumerRecord.value());
    }
}
